// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';

//导入全局应用实例
const app = getApp();

//导入外部js脚本
const common = require("../../common.js");

Page({
  data: {
    author: app.globalData.author,
    motto: '小哟智能家居',
    "mottoColor": '#F05B72',
    "mottoBgColor": '#F05B72',
    "myName": null,
    "devices": ["花卉检测仪","环境检测仪","智能花盆"],
    "deviceIdx": 0,
    "xb":null,
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    cardCur: 0,
      swiperList: [{
        id: 0,
        url: 'https://5b0988e595225.cdn.sohucs.com/images/20171204/53903cb30fbf449a86eeeff074f17c92.jpeg'
      }, {
        id: 1,
        url: 'https://img1.baidu.com/it/u=3589204007,1634222711&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      }, {
        id: 2,
        url: 'https://pic.rmb.bdstatic.com/bjh/down/8d104fd0eae1acd2418381be4b96679f.png'
      }, {
        id: 3,
        url: 'https://img2.baidu.com/it/u=1583164435,1254765926&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
      }, {
        id: 4,
        url: 'https://img0.baidu.com/it/u=2374101761,3329767059&fm=253&fmt=auto&app=138&f=PNG?w=884&h=500'
      }, {
        id: 5,
        url: 'https://img0.baidu.com/it/u=3165135743,3820841171&fm=253&fmt=auto&app=138&f=JPG?w=530&h=408'
      }, {
        id: 6,
        url: 'https://img2.baidu.com/it/u=3158249589,3521391675&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500'
      }],

  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  /**
   *页面控件的用户事件绑定的函数名，是定义在js脚本文件中的页面对象
  */
  //捕获文本框内容的输入
  inputText: function(evt){
    //console是调试器的控制台窗口对象，其log函数用于打印输出
    //console.log(evt); //打印事件体，观察事件发生的细节
    let id = evt.target.id; //获取事件源的id属性值
    let text = evt.detail.value; //获取事件源里的值
    this.setData({[id]:text});
    //console.log(id + "# " + text);
  },
  //性别选项变化时
  deviceChange: function(evt){
    //console.log(evt); //打印事件体，观察事件发生的细节
    this.setData({deviceIdx:evt.detail.value});
    //console.log("#" + Math.random());
  },
  //捕获点我按钮的
  tapMe: function(evt){
    //console是调试器的控制台窗口对象，其log函数用于打印输出
    //console.log("#" + Math.random()); //Math是数字对象，random是随机函数（返回 0 <= x < 1）
    let color = randomColor();
    let bgColor = randomColor();
    //console.log(evt); //打印事件体，观察事件发生的细节
    this.setData({mottoColor:color,mottoBgColor:bgColor})
    let name = this.data.myName;
    let xb = this.data.devices[this.data.deviceIdx];
    let text = "设备： " + xb + "\r\n设备代码：" + name;
    //console.log(evt.detail.value);
    app.alert(text);
    //let who = evt.target.dataset.who;
    //let title = '点了我' + who;
    //common.toast(title);
    //console.log(evt);
  },
  //页面跳转
  goPage: function(evt){
    //console.log("#" + Math.random()); //Math是数字对象，random是随机函数（返回 0 <= x < 1）
    let myName = this.data.myName;
    let device = this.data.deviceIdx;
    if(device == 0)
    {
      let link = "/pages/device/device1/device1?device=" + device + "&myName=" + myName;
      wx.navigateTo({url: link})
    }
    else if(device == 1)
    {
      let link = "/pages/device/device2/device2?device=" + device + "&myName=" + myName;
      wx.navigateTo({url: link})
    }
    console.log(device);
    
  },
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  }
})

function randomColor() {
  var rand = (Math.floor(Math.random() * 0xffffff)).toString(16);
  if(rand.length == 6){
    return "#" + rand;
  }else{
    return randomColor(); //当不足6位长度时，递归调用
  }
}
